using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace BloodThirsty
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class MainGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        // khoi tao lop button
        Button Button2D;

        // Khoi tao lop menu
        Menu Menu2D;
        
        // Khoi tao Lop Fire

        SpriteFire spriteFire;

        // Khoi tao class load resource
        LoadResource loadReSource ;

        //Khoi taolop demon
        CharacterDemon DeMon;

        // Khoi tao class CacBienChuongTrinh
        CacBienCuaChuongTrinh LuuBien =new CacBienCuaChuongTrinh();

        //Khoi tao class Map
        Map MapD;

        public MainGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // The hien full man hinh
            this.graphics.IsFullScreen = true;
            // Lam cho nhin thay chuot
            this.IsMouseVisible = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);


            // Khoi tao class LoadResource voi content chinh la content cua main game
            loadReSource = new LoadResource(this.Content);


            //thiet lap vi tri ban dau cho cac background
            LuuBien.Nen = loadReSource.LoadBackGround();
            LuuBien.Nen1 = loadReSource.LoadBackGround1();

            //Thiet lap vi tri ban dau cho cac menu
            
            Menu2D = new Menu(loadReSource.LoadMenu(), 920, -400);


            //Thiet lap vi tri ban dau cho cac button
            Button2D = new Button(loadReSource.LoadMangButton(), -250, 235);

            // Khoi tao fire
            spriteFire = new SpriteFire(loadReSource.LoadMangFire(), -100, -100);

            //Khoi tao Demon
            DeMon = new CharacterDemon(loadReSource.LoadDemonStand(),loadReSource.LoadDemonMove(),600,300);

            // Khoi tao map
            MapD = new Map(loadReSource.LoadMangMap(), 100, 100);

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            //Update cho menu
            Menu2D.Update(gameTime);
            // Uppdate cho button
            if (Menu2D.DaThucHienXong() == 1)
            {
                Button2D.Update(gameTime);
                spriteFire.Update(gameTime);
            }
            MouseState ms = Mouse.GetState();

            Button2D.State = Button2D.IsSelected(ms.X, ms.Y);
            // Khi vao Tro choi
            if (LuuBien.BienMenu == 1)
            {
                
                
                DeMon.Update(gameTime);
                DeMon.HChuyenDong = DeMon.HuongChuyenDong(ms.X, ms.Y);
                MapD.HuongChuyenDongMap = DeMon.HuongChuyenDong(ms.X, ms.Y);
                MapD.Update(gameTime);
            }
            
            
            if (ms.LeftButton == ButtonState.Pressed)
            {
                
                spriteFire = new SpriteFire(loadReSource.LoadMangFire(), ms.X -25, ms.Y -25);
                // Bat dau tro choi
                if (Button2D.State == 1)
                {
                    LuuBien.BienMenu = 1;
                    
                    Button2D.KetThucMenu = true;
                    

                }
                // De thoat khoi chuong trinh
                
                if (Button2D.State == 6)
                {
                    this.Exit();
                }
            }
            if (ms.LeftButton == ButtonState.Released)
            {
                
            }
            
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            if (LuuBien.BienMenu != 1)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);

                // Vi tri ban dau hinh nen
                Rectangle vitri = new Rectangle(0, 0, this.GraphicsDevice.DisplayMode.Width, this.GraphicsDevice.DisplayMode.Height);
                spriteBatch.Begin();
                // Ve background
                spriteBatch.Draw(LuuBien.Nen1, vitri, Color.White);


                //Ve Menu
                Menu2D.Draw(gameTime, spriteBatch);

                // Ve cac button
                Button2D.Draw(gameTime, spriteBatch);


                // Ve Fire
                spriteFire.Draw(gameTime, spriteBatch);
                spriteBatch.End();
            }
            // Khi dang o trong tro choi
            else
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);

                spriteBatch.Begin();
                // ve map
                MapD.Draw(gameTime, spriteBatch);

                // Ve quai vat
                DeMon.Draw(gameTime, spriteBatch);

                spriteBatch.End();
            }

            base.Draw(gameTime);
        }
    }
}
